package com.work.cos.web.controller.api.pr;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.work.cos.domain.entity.pr.PrCartProductEntity;
import com.work.cos.domain.entity.pr.PrProductEntity;
import com.work.cos.domain.vo.ComServiceResVo;
import com.work.cos.domain.vo.pr.CartDetailVo;
import com.work.cos.service.interfc.pr.IPrCartProductService;
import com.work.cos.service.interfc.pr.IPrProductService;

@RestController
@RequestMapping("/api/cart")
public class PrCartProductApiController {
	
	private static Logger LOGGER = LoggerFactory.getLogger(PrCartProductApiController.class);
	
	@Autowired
	private IPrCartProductService cartProductService;
	@Autowired
	private IPrProductService productService;

	/**
	 * 获取购物车
	 * @return
	 */
	@PostMapping(value = "/info")
	public ComServiceResVo getCartProductInfo(@RequestParam Long userId){
		LOGGER.debug("get cart product... ");
		List<CartDetailVo> cartDetailArray = new ArrayList<CartDetailVo>();
		List<PrCartProductEntity> cartArrays = cartProductService.getList(userId);
		for(PrCartProductEntity temp : cartArrays){
			PrProductEntity productEntity = productService.getOne(temp.getProductId());
			if(null != productEntity){
				CartDetailVo detailVo = new CartDetailVo();
				detailVo.setCartVo(temp);
				detailVo.setProductVo(productEntity);
				cartDetailArray.add(detailVo);
			}
		}
		
		return ComServiceResVo.returnList(cartDetailArray);
	}
	
	/**
	 * 添加购物车
	 * @return
	 */
	@PostMapping(value = "/add")
	public ComServiceResVo addCartProduct(@RequestParam Long userId, 
			@RequestParam Long productId, @RequestParam Integer number){
		LOGGER.debug("add cart product... ");
		Boolean addFlag = true;
		//如果追加商品相同,则数量相加
		PrCartProductEntity entity = cartProductService.findCartProduct(userId, productId);
		if(null != entity){
			entity.setNumber(entity.getNumber() + number);
			addFlag = cartProductService.updateNumer(entity);
		}else{
			PrCartProductEntity addEntity = new PrCartProductEntity();
			addEntity.setProductId(productId);
			addEntity.setUserId(userId);
			addEntity.setNumber(number);
			addFlag = cartProductService.save(addEntity);
		}
		
		if(addFlag){
			
			return ComServiceResVo.returnSuccessStatus();
		}

		return ComServiceResVo.returnErrorMessage("添加购物车失败!");
	}
	
	/**
	 * 编辑购物车
	 * @return
	 */
	@PostMapping(value = "/edit")
	public ComServiceResVo editCartProduct(@RequestParam Long id,@RequestParam Long userId, 
			@RequestParam Long productId, @RequestParam Integer number){
		LOGGER.debug("edit cart product... ");
		boolean editFlag = true;
		//一旦数量为零, 则移除购物车
		if("0".equals(number)){
			PrCartProductEntity entity = new PrCartProductEntity();
			entity.setId(id);
			entity.setProductId(productId);
			entity.setUserId(userId);
			editFlag = cartProductService.deleteByCheck(entity);
		}else{
			
			PrCartProductEntity entity = new PrCartProductEntity();
			entity.setId(id);
			entity.setProductId(productId);
			entity.setUserId(userId);
			entity.setNumber(number);
			
			editFlag = cartProductService.updateNumer(entity);
		}
		
		if(editFlag){
			
			return ComServiceResVo.returnSuccessStatus();
		}

		return ComServiceResVo.returnErrorMessage("更新失败");
	}
	
	/**
	 * 移除购物车
	 * @return
	 */
	@PostMapping(value = "/del")
	public ComServiceResVo queryFirstList(@RequestParam Long id,@RequestParam Long userId, 
			@RequestParam Long productId){
		
		PrCartProductEntity entity = new PrCartProductEntity();
		entity.setId(id);
		entity.setProductId(productId);
		entity.setUserId(userId);
		Boolean delFlag = cartProductService.deleteByCheck(entity);
		if(delFlag){
			
			return ComServiceResVo.returnSuccessStatus();
		}

		return ComServiceResVo.returnErrorMessage("删除失败");
	}
	
	/**
	 * 批量移除购物车
	 * @return
	 */
	@PostMapping(value = "/del/more")
	public ComServiceResVo queryFirstList(List<PrCartProductEntity> arrays){
		
		for(PrCartProductEntity temp : arrays){
			
			cartProductService.deleteByCheck(temp);
		}

		return ComServiceResVo.returnSuccessStatus();
	}

}
